﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Drawing;
/***********************************************************************
 * Create By: Max.Tan
 * Create Datetime:2014/02/12 14:55:15
 * Description:
 * 
 * Change History:
 * 
 * 用法： 将png格式、背景色透明的从0开始命名的片图放到exe所在目录，运行exe
 * 
 * *********************************************************************/
namespace ComImage
{
    class Program
    {
        static void Main(string[] args)
        {
			string appPath = System.IO.Directory.GetCurrentDirectory();
			ComImage comImage = new ComImage();
			foreach (string filePath in System.IO.Directory.GetFiles(appPath))
			{
				string _extension = System.IO.Path.GetExtension(filePath);//获取文件扩展名
				if (_extension == ".gif" || _extension == ".jpg" || _extension == ".png" || _extension == ".jpeg")
				{
					Console.WriteLine(filePath);
					comImage.AddImage(filePath);
				}
			}
			if (comImage.ImageList.Count < 1)
				Console.WriteLine("没有要合成的图片！");
			else
			{
				comImage.SaveImage();
				Console.WriteLine("图片合成完毕！总共{0}张图片", comImage.ImageList.Count);
			}
			Console.ReadKey();
        }
    }

    class ComImage
    {
        public IList<Image> ImageList;

        public ComImage()
        {
            ImageList = new List<Image>();
        }

        public void AddImage(string FilePath)
        {
            ImageList.Add(GetImage(FilePath));
        }

        public void SaveImage()
        {
            Bitmap bitmap = new Bitmap(ImageList.Count * ImageList[0].Width, ImageList[0].Height);
            using (Graphics g = Graphics.FromImage(bitmap))
            {
                for (int i = 0; i < ImageList.Count; i++)
                {
                    g.DrawImage(ImageList[i], new Point(i * ImageList[i].Width, 0));
                }
            }
            bitmap.Save("ComImage.png");
        }

        public Image GetImage(string FilePath)
        {
            Image Img = null;
            using (MemoryStream Ms = new MemoryStream(File.ReadAllBytes(FilePath)))
            {
                Img = Image.FromStream(Ms);
            }
            return Img;
        }
    }  
}
